package com.chencai.wiki.database.utils;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.chencai.wiki.database.googleapp.PersistenceManagerGenerator;
import com.chencai.wiki.database.googleapp.TablePage;
import com.google.appengine.api.datastore.Text;

/**
 * the interface of accessing the database 
 * @author chencai
 *
 */
public class DataAccess 
{
	
	private DataAccess()
	{
		
	}
	
	private static DataAccess instance = null;
	
	public static DataAccess getDataAccess()
	{
		if(instance == null)
		{
			instance = new DataAccess();
		}
		return instance;

	}
	
	/**
	 * insert a new item into the Page Table 
	 * @param title 	the item name
	 * @param author	the author of the item
	 * @param content	the content of the item
	 * @return return true if it is processed successfully,
	 * 			otherwise, return false
	 */
	public boolean insertPage(String title, String author, String content)
	{
    	PersistenceManager pm = PersistenceManagerGenerator
    			.getPersistentceManagerFactory()
    			.getPersistenceManager();
    	
    	Text tContent = new Text(content);
    	
        List<TablePage> pages = (List<TablePage>) pm
        		.newQuery(TablePage.class, "title == '" + title + "'").execute();
        if(pages.size() > 0)
        {
        	TablePage olditem = pages.get(0);
        	olditem.setAuthor(author);
        	olditem.setContents(tContent);
        	olditem.setCreateDate(new Date());
        	olditem.setTitle(title);
        	pm.close();
        	return false;
        }
        
        TablePage item = new TablePage(title, author, new Date(), tContent);
        pm.makePersistent(item);
        pm.close();
     
        
		return true;
	}
	
	/**
	 * get specified page
	 * @param title the page title
	 * @return the page content, if failed, return null
	 */
	public String getPage(String title)
	{
		//System.out.println("try get page: " + title);
    	PersistenceManager pm = PersistenceManagerGenerator
			.getPersistentceManagerFactory()
			.getPersistenceManager();
    	List<TablePage> pages = (List<TablePage>) pm
 			.newQuery(TablePage.class, "title == '" + title + "'").execute();
    	if(pages.size() == 0){
    		return null;
    	}
    	String ret = pages.get(0).getContents().getValue();
    	pm.close();
    	return ret;
   	}
	
	//it is for testing
	public static String showTable()
	{
    	PersistenceManager pm = PersistenceManagerGenerator
    			.getPersistentceManagerFactory()
    			.getPersistenceManager();
    	String query = "select from " + TablePage.class.getName();
        List<TablePage> pages = (List<TablePage>) pm.newQuery(query).execute();
        String ret = "";
    	ret += pages.size();
        
        for(TablePage tpg : pages)
    	{
    		ret += "\n****************\n\t" + tpg.getPageID() + "\n\t" + tpg.getAuthor()+ " \n\t" + tpg.getTitle() + " \n\t" + tpg.getCreateDate().toString() + "\n\t " + tpg.getContents();
    		
    		pm.makePersistent(tpg);
    		
    		
    	}
    	
    	pm.close();
    	return ret;

	}
	
	//it is for testing
	public static void test()
	{
		DataAccess da = new DataAccess();
		da.insertPage("baidu", "chencai", "baidu is a top search engine company in China.");
	}
	
}
